package com.chj.service;

import com.chj.dao.UserEntityMapper;
import com.chj.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserEntityMapper userMapper;

    @Transactional
    public long addOne(UserEntity entity) throws Exception {
        if(StringUtils.isEmpty(entity.getUserNum())){
            throw new Exception();
        }
        userMapper.insert(entity);
        return entity.getId();
    }

    public List<UserEntity> getList() {
        return userMapper.selectByMap(new HashMap<>());
    }

    public UserEntity getOneByUserNum (String userNum){
        Map<String,Object> userQueryMap = new HashMap<>();
        userQueryMap.put("userNum", userNum);
        List<UserEntity> users = userMapper.selectByMap(userQueryMap);
        return users.size() == 1 ? users.get(0): null;
    }


}
